<?php
namespace Home\Controller;
use Common\Common\Classes\Message;
use Home\Model\ClassService;
use Home\Model\ClassStudentService;
use Home\Model\QuestionService;
use Home\Model\QuestionSetService;
use Home\Model\StudentQuestionService;
use Home\Model\StudentResultService;
use Think\Controller;

class QuizResultController extends HomeController{
    //---------------------------------跳转页面(同时返回班级信息)----------------------------------------------
    public function grade(){
        $cla_sql = new ClassService();
        $check = $cla_sql->checkPower();
        if($check->status == 1){
            $classInfo = $cla_sql->classInfo();         //获得班级的基本信息
            $this->assign("classInfo",$classInfo);
            $this->display();
        }
        else{
            $this->error($check->message);
        }
    }
    //---------------------------------获得试卷和学生成绩----------------------------------------------
    public function getAllGrades(){
        if(IS_AJAX) {
            $_GET["cad"] = $_POST["cla_id"];
            $cla_sql = new ClassService();
            $check = $cla_sql->checkPower();
            if($check->status == 1) {
                $cs_sql = new ClassStudentService();
                $pageInfo = $cla_sql->getPageInfo();
                $stu_field = array("student_stu_id");
                $students = $cs_sql->unsetStu($cs_sql->getClassStuInfo($pageInfo["np"],$stu_field,0));     //获得所有学生
                $qs_sql = new QuestionSetService();
                $quiz = $qs_sql->getAllOpenQuiz();              //获得所有试卷
                $sr_sql = new StudentResultService();
                for ($i = 0; $i < count($students); $i++) {           //整理学生格式(加上学生成绩)
                    $_POST["stu_id"] = $students[$i]["stu_id"];
                    for ($j = 0; $j < count($quiz); $j++) {
                        $_POST["qus_id"] = $quiz[$j]["qus_id"];
                        $students[$i]["allResult"][$j] = $sr_sql->getStuQuizResult();
                    }
                }
                $d["pageInfo"] = $pageInfo;
                $d["quizs"] = $quiz;
                $d["students"] = $students;
                $data = new Message(1, "", $d);
            }
            else{
                $data = new Message(0,"你没有权限对该班级进行操作!","");
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------搜索某些学生的成绩情况----------------------------------------------
    public function getSomeGrades(){
        if(IS_AJAX) {
            $_GET["cad"] = $_POST["class_cla_id"];        //转换一下,用在检测班级权限上
            $cla_sql = new ClassService();
            $check = $cla_sql->checkPower();
            if ($check->status == 1) {
                $cs_sql = new ClassStudentService();
                $students = $cs_sql->unsetStu($cs_sql->searchStu());       //获得所有符合条件的学生,并且unsetStu处理格式
                $qs_sql = new QuestionSetService();
                $quiz = $qs_sql->getAllOpenQuiz();              //获得所有试卷
                $sr_sql = new StudentResultService();
                for ($i = 0; $i < count($students); $i++) {           //整理学生格式(加上学生成绩)
                    $_POST["stu_id"] = $students[$i]["stu_id"];
                    for ($j = 0; $j < count($quiz); $j++) {
                        $_POST["qus_id"] = $quiz[$j]["qus_id"];
                        $students[$i]["allResult"][$j] = $sr_sql->getStuQuizResult();
                    }
                }
                $d = $students;     //仅仅需要学生的所有成绩
                $data = new Message(1, "", $d);
            }
            else {
                $data = new Message(0, $check->message, "");
            }
            $this->ajaxReturn($data);
        }
    }
    //----------------------------------跳转到测试分析---------------------------------------------
    public function analyze(){
        $_POST["qus_id"] = $_GET["qd"];
        $qs_sql = new QuestionSetService();
        $quiz_field = array("class_cla_id");
        $class = $qs_sql->getQuizInfo($quiz_field);
        $check = $qs_sql->checkPower();
        if($check->status == 1) {
            $this->assign("qd",$_GET["qd"]);
            $this->assign("cad",$class["class_cla_id"]);
            $this->display();
        }
        else{
            $this->error($check->message);
        }
    }
    //----------------------------------获得试卷的结果统计---------------------------------------------
    public function countResult(){
        if(IS_AJAX) {
            $qs_sql = new QuestionSetService();
            $check = $qs_sql->checkPower();
            if ($check->status == 1) {
                $que_sql = new QuestionService();
                $d["total_score"] = $que_sql->countScore();      //获得试卷的总分
                $d["group"] = $que_sql->grouping($d["total_score"]);      //获得分组
                $sr_sql = new StudentResultService();
                for ($i = 0; $i < count($d["group"]); $i++) {            //查询每个分组的人数,并且将分组数据整理好!range是分组范围   num是人数
                    if ($d["group"][$i][0] == $d["group"][$i][1]) {
                        $d["group"][$i]["range"] = $d["group"][$i][0];
                    } else {
                        $d["group"][$i]["range"] = $d["group"][$i][0] . "-" . $d["group"][$i][1];
                    }
                    $_POST["stu_score"] = array("between", $d["group"][$i]);
                    $d["group"][$i]["num"] = $sr_sql->getSomeScoreNum();
                    unset($d["group"][$i][0]);
                    unset($d["group"][$i][1]);
                }
                unset($_POST["stu_score"]);
                $quiz_field = array("qus_id","qus_name","qus_status","qus_create_time","class_cla_id");
                $d["quiz"] = $qs_sql->getQuizInfo($quiz_field);     //获得试卷基本信息
                $_GET["cad"] = $d["quiz"]["class_cla_id"];
                $cla_sql = new ClassService();
                $class = $cla_sql->classInfo();
                $d["stu_num"] = $class["cla_stu_num"];      //获得班级人数
                $d["submitNum"] = $sr_sql->getSubmitNum();           //获得交卷人数
                $_POST["stu_score"] = array("egt", ceil($d["total_score"] * 0.6));      //整理查询合格分数的条件语句
                $d["passNum"] = $sr_sql->getPassNum();           //获得合格的人数
                unset($_POST["stu_score"]);
                $d["max"] = $sr_sql->getMaxScore();         //获得最高分
                $d["min"] = $sr_sql->getMinScore();         //获得最低分
                $d["avg"] = $sr_sql->getAvgScore();         //获得平均分
                $d["datetime"] = $sr_sql->getTime();        //获得当前事件
                $que_field = array("que_id","que_type","que_content","que_num","que_item1","que_item2","que_item3","que_item4","que_item5",
                    "que_item6","que_item7","que_item8","que_answer","que_score");
                $question = $que_sql->unsetQusetion($que_sql->getQuizQuestion($que_field));
                unset($_POST);
                for ($i = 0; $i < count($question); $i++) {
                    $_POST["que_id"] = $question[$i]["que_id"];
                    $sq_sql = new StudentQuestionService();
                    $question[$i]["accuracy"] = $sq_sql->countQuestionAccuracy();           //获得题目的正确率
                    for ($j = 0; $j < count($question[$i]["que_item"]); $j++) {     //计算题目的选择人数
                        $_POST["stu_answer"] = array("like","%".$j."%");
                        $question[$i]["que_item"][$j]["num"] = $sq_sql->countItemSubmitNum();           //选择人数
                        $question[$i]["que_item"][$j]["per"] = $sq_sql->countItemPer($question[$i]["que_item"][$j]["num"]);     //所占百分比
                    }
                }
                $d["questions"] = $question;
                $data = new Message(1, "", $d);
            } else {
                $data = new Message(0, $check->message, "");
            }
            $this->ajaxReturn($data);
        }
    }

}